package com.xmb.wechat.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xmb.wechat.R;
import com.xmb.wechat.delegate.ChartBarExtendDelegate;
import com.xmb.wechat.entity.ChartBarEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/11 16:57
 */
public class ChartBarExtend extends LinearLayout {
    private View                         mView;
    private onChatBarExtendClickCallBack mCallBack;
    private ChartBarExtendDelegate       mDelegate;

    public ChartBarExtend(Context context) {
        this(context, null);
    }

    public ChartBarExtend(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChartBarExtend(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.layout_recycler_view, null, false);
        mDelegate = new ChartBarExtendDelegate(context, mView.findViewById(R.id.recyclerView));
        mDelegate.addData(getChartBarData());
        mDelegate.setOnItemClick(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mCallBack == null)
                    return;
                ChartBarEntity chartBarEntity = (ChartBarEntity) mDelegate.getData().get(position);
                switch (chartBarEntity.getType()) {
                    case 0:
                        mCallBack.onPictureClick();
                        break;
                    case 1:
                        mCallBack.onVoiceClick();
                        break;
                    case 2:
                        mCallBack.onVideoCallClick();
                        break;
                    case 3:
                        mCallBack.onRedPacketClick();
                        break;
                    case 4:
                        mCallBack.onZhuanZhangClick();
                        break;
                    case 5:
                        mCallBack.onTimeClick();
                        break;
                    case 6:
                        mCallBack.onSystemTipClick();
                        break;
                    case 7:
                        mCallBack.onBussinessClick();
                        break;
                    case 8:
                        mCallBack.onRedPacketReceive();
                        break;
                }
            }
        });
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(mView, lp);
    }

    private List<ChartBarEntity> getChartBarData() {
        ArrayList<ChartBarEntity> results = new ArrayList<>();
        results.add(new ChartBarEntity("图片", R.drawable.wechat_chat_add_open_pickimg, 0));
        results.add(new ChartBarEntity("语音", R.drawable.wechat_chat_add_open_voice, 1));
        results.add(new ChartBarEntity("通话", R.drawable.wechat_chat_add_open_video_call, 2));
        results.add(new ChartBarEntity("发红包", R.drawable.wechat_chat_add_open_redpkg, 3));
        results.add(new ChartBarEntity("收红包", R.drawable.wechat_chat_add_open_redpkg, 8));
        results.add(new ChartBarEntity("转账", R.drawable.wechat_chat_add_open_zhuanzhang, 4));
        results.add(new ChartBarEntity("名片", R.drawable.wechat_business_cards, 7));
        results.add(new ChartBarEntity("时间", R.drawable.wechat_chat_add_open_time, 5));
        results.add(new ChartBarEntity("系统", R.drawable.wechat_chat_add_open_systip, 6));
        return results;
    }

    public void init(onChatBarExtendClickCallBack onChatBarExtendClickCallBack, boolean isRoomChat) {
        mCallBack = onChatBarExtendClickCallBack;
        if (isRoomChat) {
            mDelegate.getData().remove(2);
            mDelegate.getData().remove(4);
        } else {
            mDelegate.getData().remove(3);
            mDelegate.getData().remove(3);
            mDelegate.getData().add(3, new ChartBarEntity("红包", R.drawable.wechat_chat_add_open_redpkg, 3));
        }
    }

    public interface onChatBarExtendClickCallBack {
        void onVoiceClick();

        void onPictureClick();

        void onRedPacketClick();

        void onZhuanZhangClick();

        void onVideoCallClick();

        void onTimeClick();

        void onSystemTipClick();

        void onBussinessClick();

        void onRedPacketReceive();

    }
}
